package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.Wallet;
import cn.wolfcode.trip.base.service.IWalletService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wallets")
public class WalletController {

    @Autowired
    private IWalletService walletService;

    @RequestMapping(value = "/{user.id}",method = RequestMethod.GET)
    public Wallet getBalanceByUserId(@PathVariable("user.id") Long id){

        return walletService.getBalanceByUserId(id);
    }

    @RequestMapping(value ="/updateBalances",method= RequestMethod.POST)
    public JsonResult updateBalance( Long userId, Integer integral){
        Wallet wallet = new Wallet();
        User user = new User() ;
        user.setId(userId);
        wallet.setUser(user);
        wallet.setIntegral(integral);
        JsonResult result = new JsonResult();
        try {
            walletService.updateBalance(wallet);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
